package com.echat.messagesender.sender.sms.impl;

import com.echat.messagesender.sender.exception.EchatSmsException;
import com.echat.messagesender.sender.sms.SmsSender;
import com.echat.messagesender.sender.sms.bean.Echat253Config;
import com.echat.messagesender.sender.bean.EchatSendSmsInfo;
import com.echat.messagesender.sender.sms.impl.baseutil.Sms253Util;
import com.echat.messagesender.sender.util.JsonUtil;
import com.echat.messagesender.sender.util.StringUtils;

import java.util.Map;

/**
 * Created by chenmohan on 2018/08/16
 *
 * @description
 */
public class Echat253Sender extends SmsSender {

    private Echat253Config config;

    public Echat253Sender(Echat253Config config) {
        config.check();
        this.config = config;
        super.platformAccountId = config.getPlatformAccountId();
        super.platformName = config.getPlatformName();
    }

    @Override
    public boolean sendSms(EchatSendSmsInfo info) {
        String[] phonoNos = info.getPhonoNos();
        for (String phonoNo : phonoNos) {
            if (StringUtils.isNotEmpty(info.getTemplateId())) {
                //使用短信模板发送
                try {
                    return Sms253Util.sendTemplateSMS(config, phonoNo, info.getTemplateId(), JsonUtil.toMap(info.getVars())) != null;
                } catch (Exception e) {
                    return sendSms(phonoNo, info.getSmsContent());
                }
            } else {
                //费短信模板发送
                sendSms(phonoNo, info.getSmsContent());
            }
        }
        return true;

    }

    @Override
    public boolean sendBatchSms(String[] receiveNos, String templateId, Map<String, String>[] templateParams) {
        throw new EchatSmsException("暂不支持此功能！");
    }

    @Override
    public boolean sendSms(String receiveNos, String content) {
        return Sms253Util.sendSimpleSMS(config, receiveNos, content) != null;

    }
}
